﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using HRMSys.DAL;
using HRMSys.Model;

namespace HRMSys.UI.SystemMgr
{
    /// <summary>
    /// BuildSalarySheet.xaml 的交互逻辑
    /// </summary>
    public partial class BuildSalarySheetUI : Window
    {
        public BuildSalarySheetUI()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //生成年份
            List<int> yearList = new List<int>();
            int curYear = DateTime.Now.Year;
            for (int i = curYear - 3; i < curYear + 3; i++)
            {
                yearList.Add(i);
            }
            cboYear.ItemsSource = yearList;

            //开始生成月份
            List<int> monthList = new List<int>();
            for (int i = 1; i <= 12; i++)
            {
                monthList.Add(i);
            }
            cboMonth.ItemsSource = monthList;

            //获得所有部门
            DepartmentDAL dal = new DepartmentDAL();
            Department[] departs = dal.GetAll(true);
            cboDepartment.ItemsSource = departs;
        }

        private void btnBuild_Click(object sender, RoutedEventArgs e)
        {
            //非空验证 ?
            //获得用户选定的值(年/月/部门)
            int year = Convert.ToInt32(cboYear.SelectedValue);
            int month = Convert.ToInt32(cboMonth.SelectedValue);
            Guid departId = (Guid)cboDepartment.SelectedValue;

            SalarySheetDAL dal = new SalarySheetDAL();
            bool isExists = dal.IsExists(year, month, departId);
            if (isExists == true)
            {

                if (MessageBox.Show("要生成的工资单已经存在, 是否覆盖?", "系统提示",
                    MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
                {
                    dal.Delete(year, month, departId);
                }
            }
            dal.Build(year, month, departId);
            //MessageBox.Show("生成工资单成功");
            EmployeeDAL employeeDAL = new EmployeeDAL();
            Employee[] employeeList = employeeDAL.GetByDepartment(departId);
            colEmployee.ItemsSource = employeeList;

            SalarySheetItem[] items = (new SalarySheetDAL()).GetSalarySheetItems(year, month, departId);
            gridSalarySheetItem.ItemsSource = items;
        }

        private void gridSalarySheetItem_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
        {
            //MessageBox.Show(e.GetType().ToString());
            SalarySheetItem item = (SalarySheetItem)e.Row.DataContext;
            (new SalarySheetDAL()).Update(item);
        }
    }
}
